home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / Regex.cc < prev    next >
C/C++ Source or Header  |  1994-08-16  |  4KB  |  141 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. /* 
  19.   Regex class implementation
  20.  */
  21.  
  22. #ifdef __GNUG__
  23. #pragma implementation
  24. #endif
  25. #include <std.h>
  26. #include <ctype.h>
  27. #include <new.h>
  28. #include <builtin.h>
  29.  
  30. extern "C" {
  31. #if 1
  32. #include <rx.h>
  33. #else
  34. #include <regex.h>
  35. #endif
  36. }
  37.  
  38. #include <Regex.h>
  39.  
  40. Regex::~Regex()
  41. {
  42.   if (buf->buffer) free(buf->buffer);
  43.   if (buf->fastmap) free(buf->fastmap);
  44.   delete(buf);
  45.   delete(reg);
  46. }
  47.  
  48. Regex::Regex(const char* t, int fast, int bufsize, 
  49.                const char* transtable)
  50. {
  51.   int tlen = (t == 0)? 0 : strlen(t);
  52.   buf = new re_pattern_buffer;
  53.   memset (buf, 0, sizeof(re_pattern_buffer));
  54.   reg = new re_registers;
  55.   if (fast)
  56.     buf->fastmap = (char*)malloc(256);
  57.   else
  58.     buf->fastmap = 0;
  59.   buf->translate = (unsigned char*)transtable;
  60.   if (tlen > bufsize)
  61.     bufsize = tlen;
  62.   buf->allocated = bufsize;
  63.   buf->buffer = (char *)malloc(buf->allocated);
  64.   const char* msg = re_compile_pattern((const char*)t, tlen, buf);
  65.   if (msg != 0)
  66.     (*lib_error_handler)("Regex", msg);
  67.   else if (fast)
  68.     re_compile_fastmap(buf);
  69. }
  70.  
  71. int Regex::match_info(int& start, int& length, int nth) const
  72. {
  73.   if ((unsigned)(nth) >= RE_NREGS)
  74.     return 0;
  75.   else
  76.   {
  77.     start = reg->start[nth];
  78.     length = reg->end[nth] - start;
  79.     return start >= 0 && length >= 0;
  80.   }
  81. }
  82.  
  83. int Regex::search(const char* s, int len, int& matchlen, int startpos) const
  84. {
  85.   int matchpos, pos, range;
  86.   if (startpos >= 0)
  87.   {
  88.     pos = startpos;
  89.     range = len - startpos;
  90.   }
  91.   else
  92.   {
  93.     pos = len + startpos;
  94.     range = -pos;
  95.   }
  96.   matchpos = re_search_2(buf, 0, 0, (char*)s, len, pos, range, reg, len);
  97.   if (matchpos >= 0)
  98.     matchlen = reg->end[0] - reg->start[0];
  99.   else
  100.     matchlen = 0;
  101.   return matchpos;
  102. }
  103.  
  104. int Regex::match(const char*s, int len, int p) const
  105. {
  106.   if (p < 0)
  107.   {
  108.     p += len;
  109.     if (p > len)
  110.       return -1;
  111.     return re_match_2(buf, 0, 0, (char*)s, p, 0, reg, p);
  112.   }
  113.   else if (p > len)
  114.     return -1;
  115.   else
  116.     return re_match_2(buf, 0, 0, (char*)s, len, p, reg, len);
  117. }
  118.  
  119. int Regex::OK() const
  120. {
  121. // can't verify much, since we've lost the original string
  122.   int v = buf != 0;             // have a regex buf
  123.   v &= buf->buffer != 0;        // with a pat
  124.   if (!v) (*lib_error_handler)("Regex", "invariant failure");
  125.   return v;
  126. }
  127.  
  128. /*
  129.  some built-in Regular expressions
  130. */
  131.  
  132. const Regex RXwhite("[ \n\t\r\v\f]+", 1);
  133. const Regex RXint("-?[0-9]+", 1);
  134. const Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?", 1, 200);
  135. const Regex RXalpha("[A-Za-z]+", 1);
  136. const Regex RXlowercase("[a-z]+", 1);
  137. const Regex RXuppercase("[A-Z]+", 1);
  138. const Regex RXalphanum("[0-9A-Za-z]+", 1);
  139. const Regex RXidentifier("[A-Za-z_][A-Za-z0-9_]*", 1);
  140.  
  141.